import Router from '@koa/router';

import response from '../response.js';
import moment from 'moment';
import path from 'path';
import logger from '../logger.js';
import {createToken} from '../auth.js';

import db from '../mysqlHelper.js';
import { router } from './db.js';
import publicMethod  from '../publicMethod.js';


router.prefix('/api');

router.post('/getTkoen', async (ctx) => {
    let body = ctx.request.body;
    if (body == null) {
        ctx.body = response.fail('参数不允许为空');
        return;
    }
    if(body.appid==null){
        ctx.body = response.fail('缺少必填字段 appid');
        return;
    }
    if(body.appsecret==null){
        ctx.body = response.fail('缺少必填字段 appsecret'); 
        return;
    }
    if(body.appid!='1234567890' || body.appsecret!='1234567890'){
        ctx.body = response.fail('appid或appsecret错误');
        return;
    }
    let token = await createToken({appid:body.appid});
    ctx.body = response.success(token, '获取token成功');
});


router.post('/charge/autoRefundAndShare', async (ctx) => {
    let body = ctx.request.body;
    if (body == null) {
        ctx.body = response.fail('参数不允许为空');
        return;
    }
    if(body.phone==null){
        ctx.body = response.fail('缺少必填字段 phone');
        return;d1
    }
    if(body.pay_amt==null){
        ctx.body = response.fail('缺少必填字段 pay_amt');
        return;
    }
    console.log('自动退款分账',body.phone,body.pay_amt,body.income,body.charge_quantity,body.id,body.project_id);
    let res =await publicMethod.autoRefundAndShare(body.phone,body.pay_amt,body.income,body.charge_quantity,body.id,body.project_id,0);
    if(res.code==0){
        console.warn('自动退款分账失败',res.msg,body.phone,body.pay_amt,body.income,body.charge_quantity,body.id,body.project_id);
        ctx.body = response.fail(res.msg);
        return;
    }
    if(res.code==1){
        ctx.body = response.success(res.msg, '退款成功');
        return;
    }

});






export { router };

